﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Read.Api.Application.Command;
using B.S.BaseData.Read.Api.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace B.S.BaseData.Read.Api.Controllers
{
    //[AllowAnonymous]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {

        private readonly IMediator mediator;
        private readonly ILogger<UserLoginCommand> logger;

        public UserController(IMediator mediator, ILogger<UserLoginCommand> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 用户显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<UserDto>>> GetUse([FromQuery] GetUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<UserLoginDto>> Handle([FromQuery] UserLoginCommand request)
        {
            try
            {
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary> 
        /// 递归显示菜单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<ApiResult<List<TreeMenuDTOs>>> GetTreeMenu([FromQuery] GetTreeMenuCommand request)
        {
            try
            {
                var sub = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                var rid = HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
                request.uid = sub;
                request.rid = rid;
                return mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 测试 ELK 日志接收报错信息的接口
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestELKLogging()
        {
            try
            {
                // 故意抛出异常
                throw new Exception("这是一个用于测试 ELK 日志的异常。");
            }
            catch (Exception ex)
            {
                // 记录异常日志
                logger.LogError(ex, "TestELKLogging 接口发生异常。");
                // 返回错误响应
                return StatusCode(500, "发生内部服务器错误。");
            }
        }
    }
}